#!/usr/bin/env python
# -*- coding:utf-8 -*-

import sys

from map_page import MapPage

def ERROR(format, *args) : print format % args

def verify_map(mapper, file_verify) :
    count_all  = 0
    count_fail = 0
    for line in file_verify.readlines() :
        count_all += 1

        f = line.split()
        if not f : continue
        url = f[0]
        result = mapper.map(None, url)
        failed = False
        for i in range(1, len(f)) :
            # prefix match, may be a BUG
            idx = result.find(f[i])
            if idx < 0 :
                failed = True
                ERROR('MISMATCH: "%s" %s', f[i], result)
        if failed : count_fail += 1

    return count_all, count_fail
    # verify_map

def main() :
    from optparse import OptionParser
    parser = OptionParser()
    parser.add_option("-v", "--verbose", dest="verbose", action="store_true", default=False,
                        help='show detail information')
    parser.add_option("", "--map", dest="map", type="string",
                        help='configure file for map')
    parser.add_option("", "--verify", dest="verify", type="string",
                        help='verify list file to test')
    (options, args) = parser.parse_args()
    if options.map is None or options.verify is None :
        parser.print_help( sys.stdout )
        return
    mapper = MapPage()
    mapper.open(options.map)

    count_all, count_fail = verify_map(mapper, open(options.verify))
    ERROR('Testing with %s, tests:%d, fails:%d', options.verify, count_all, count_fail)

    mapper.close()

    # main()

if __name__=='__main__' :
    main()

    sys.exit(0)
